package com.gin.kotlin.no_gay_alliance.fragment.pm

import android.view.View
import com.gin.kotlin.no_gay_alliance.adapter.pm.PrivateMessageBlackListAdapter
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingFragment
import com.gin.kotlin.no_gay_alliance.callback.RCallback
import com.gin.kotlin.no_gay_alliance.databinding.FragmentPrivateMessageBlackListBinding
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.PrivateMessageBlackListProvider
import com.gin.nga.response.user.SimpleUserInfo

class PrivateMessageBlackListFragment : AbstractBindingFragment<FragmentPrivateMessageBlackListBinding>(), RCallback<List<SimpleUserInfo>, Nothing> {
    private lateinit var adapter: PrivateMessageBlackListAdapter

    override fun onCreateView() {
        val b = this.b ?: return
        b.swipeRefreshLayout.setOnRefreshListener { request(true) }
        this.adapter = PrivateMessageBlackListAdapter(requireContext(), handler!!)
        b.recyclerView.adapter = this.adapter
        request()
    }


    fun request(ignoreCache: Boolean = false) {
        val b = this.b ?: return
        b.swipeRefreshLayout.isRefreshing = true
        b.tvWarning.visibility = View.GONE

        PrivateMessageBlackListProvider.getInstance().enqueue(ignoreCache = ignoreCache, callback = this)
    }

    override fun onSuccess(data: List<SimpleUserInfo>, ignoreCache: Boolean) {
        val b = this.b ?: return
        b.swipeRefreshLayout.isRefreshing = false
        this.adapter.replaceData(data)
    }

    override fun onError(errorMessage: String) {
        val b = this.b ?: return
        b.swipeRefreshLayout.isRefreshing = false
        b.tvWarning.visibility = View.VISIBLE
        b.tvWarning.text = errorMessage
    }
}